package com.tebiecloud.act.server.controller.feign;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.tebiecloud.act.client.model.entity.Act;
import com.tebiecloud.act.client.model.entity.ActGoodsRef;
import com.tebiecloud.act.client.service.IActGoodsRefServiceClient;
import com.tebiecloud.act.server.service.ActGoodsRefService;
import com.tebiecloud.act.server.service.ActService;
import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.common.utils.ResultBodyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.management.Query;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/feign/actgoods")
public class GoodsRefController implements IActGoodsRefServiceClient {

    @Autowired
    ActGoodsRefService actGoodsRefService;

    @Autowired
    ActService actService;

    /**
     * 获取商品信息
     * @param actId
     * @param goodsSpecificationId
     * @return
     */
    @PostMapping("listActGoodsRef")
    @Override
    public ResultBody<List<ActGoodsRef>> listActGoodsRef(@RequestParam("actId") long actId,
                                                         @RequestParam("goodsSpecificationId") long goodsSpecificationId) {
        QueryWrapper<ActGoodsRef> actGoodsRefQueryWrapper = new QueryWrapper<>();
        actGoodsRefQueryWrapper.eq("act_id",actId);
        actGoodsRefQueryWrapper.eq("spec_id",goodsSpecificationId);
        actGoodsRefQueryWrapper.eq("deleted",0);
        List<ActGoodsRef> actGoodsRefList = actGoodsRefService.list(actGoodsRefQueryWrapper);
        return ResultBodyUtils.ok(actGoodsRefList);
    }

    /**
     * 获取商品关联的活动列表
     * @param goodsId
     * @param actType
     * @return
     */
    @PostMapping("listActGoodsRefByGoodsId")
    @Override
    public ResultBody<List<ActGoodsRef>> listActGoodsRefByGoodsId(@RequestParam("goodsId") long goodsId,
                                                                  @RequestParam(value = "actType",required = false) Integer actType) {
        //排除未到时间和已过期和禁用的限时促销活动
        List<Long> actIds = new ArrayList<>();
        QueryWrapper<Act> actQueryWrapper = new QueryWrapper<>();
        actQueryWrapper.eq("status",1);
        actQueryWrapper.lt("shelf_time",new Date());
        actQueryWrapper.gt("end_time",new Date());
        if(actType!=null)actQueryWrapper.eq("act_type",actType);
        List<Act> actList = actService.list(actQueryWrapper);
        for(Act act:actList){
            actIds.add(act.getActId());
        }
        QueryWrapper<ActGoodsRef> actGoodsRefQueryWrapper = new QueryWrapper<>();
        actGoodsRefQueryWrapper.eq("goods_id",goodsId);
        actGoodsRefQueryWrapper.eq("deleted",0);
        List<ActGoodsRef> actGoodsRefList = new ArrayList<>();
        if(actList.size()>0){
            actGoodsRefQueryWrapper.in("act_id",actIds);
            actGoodsRefQueryWrapper.groupBy("act_id");
            actGoodsRefList = actGoodsRefService.list(actGoodsRefQueryWrapper);
        }
        return ResultBodyUtils.ok(actGoodsRefList);
    }

    /**
     * 获取某个活动的某个商品规格列表
     * @param actId
     * @param goodsId
     * @return
     */
    @PostMapping("listActGoodsRefByActIdAndGoodsId")
    @Override
    public ResultBody<List<ActGoodsRef>> listActGoodsRefByActIdAndGoodsId(@RequestParam("actId") long actId,
                                                                          @RequestParam("goodsId") long goodsId) {
        QueryWrapper<ActGoodsRef> actGoodsRefQueryWrapper = new QueryWrapper<>();
        actGoodsRefQueryWrapper.eq("act_id",actId);
        actGoodsRefQueryWrapper.eq("goods_id",goodsId);
        actGoodsRefQueryWrapper.eq("deleted",0);
        List<ActGoodsRef> actGoodsRefList = actGoodsRefService.list(actGoodsRefQueryWrapper);
        return ResultBodyUtils.ok(actGoodsRefList);
    }
}
